<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<title>地图</title>
<link href="${pageContext.request.contextPath}/css/map.css" rel="stylesheet" />
</head>
<body>
        <div>
             <div id="container" style="width: 100%;height: 1100px"></div>
        </div>
        <div>
           <table class="maptab">
           <form action="${pageContext.request.contextPath }/searchbath.action">
           <!-- <input type="text" name="userid" value="${USER_SESSION_USER.userid}"  style="display:none;">
           <tr><td><input type="text" id="userlng" name="userlng"></td></tr>
           <tr><td><input type="text" id="userlat" name="userlat"></td></tr> -->
           <tr><td><input type="text" id="userprovince" name="userprovince"></td></tr>
           <tr><td><input type="text" id="usercity" name="usercity"></td></tr>
           <tr><td><input type="text" id="userdistrict" name="userdistrict"></td></tr>
           <tr><td colspan="2"><input type="submit" value="定位" style="border:none;background-color:rgb(153, 184, 202);width:320px;color:white;height:50px;"></td></tr>
           </form>
           </table>
        </div>
        <script src="https://webapi.amap.com/maps?v=1.4.13&key=f2aec46249fc527def835b2e88229ccc"></script>
        <script>
        function init() {
        	var map = new AMap.Map('container', {
                resizeEnable: true,
            });
        	
        	 AMap.plugin('AMap.Geolocation', function() {
                 var geolocation = new AMap.Geolocation({
                 // 是否使用高精度定位，默认：true
                enableHighAccuracy: true,
                // 设置定位超时时间，默认：无穷大
                timeout: 10000,
                // 定位按钮的停靠位置的偏移量，默认：Pixel(10, 20)
                buttonOffset: new AMap.Pixel(10, 20),
                //  定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
                zoomToAccuracy: true,     
                //  定位按钮的排放位置,  RB表示右下
                buttonPosition: 'RB'
              })
              map.addControl(geolocation);
              geolocation.getCurrentPosition()
              AMap.event.addListener(geolocation, 'complete', onComplete)
              AMap.event.addListener(geolocation, 'error', onError)

              function onComplete (obj) {
                var res ='经纬度：' + obj.position + '\n精度范围：' + obj.accuracy + '米\n定位结果的来源：' + obj.location_type + '\n状态信息：' + obj.info + '\n地址：' + obj.formattedAddress + '\n地址信息：' + JSON.stringify(obj.addressComponent, null, 4); 
                //alert(res);
                //document.getElementById("userlng").value=obj.position.lng
                //document.getElementById("userlat").value=obj.position.lat
                document.getElementById("userprovince").value=obj.addressComponent.province
                document.getElementById("usercity").value=obj.addressComponent.city
                document.getElementById("userdistrict").value=obj.addressComponent.district
              }
            });
        }
        window.onload = init;
        </script>
</body>
</html>